package com.googlecode.cswish.struts;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.googlecode.cswish.util.InvocationUtil.ServiceInfo;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 * @author Jerry.Feng 2008-12-6
 */
public class BaseActionSupport extends ActionSupport implements DynamicAction {

	private Map<String, Object> parameters = new HashMap<String, Object>();
	
	// Reduce the duplicate key
	@Override
	public String getText(String key) {
		return super.getText(formatKey(key));
	}
	
	@Override
	public String getText(String key, String defaultValue, List args, ValueStack stack) {
		return super.getText(formatKey(key), defaultValue, args, stack);
	}
	
	public String getTextByLocale(String aTextName, String locale) {
		return super.getText(aTextName);
	}
	
	private String formatKey(String key) {
		int offset = 0;
		if (key.startsWith("execute")) {
			offset = "execute".length();
			if (key.startsWith(".data", offset)) {
				offset += ".data".length();
			}
		}
		if (offset > 0) {
			key = "model" + key.substring(offset);
		}
		return key;
	}

	@Override
	public int size() {
		return parameters.size();
	}

	@Override
	public boolean isEmpty() {
		return parameters.isEmpty();
	}

	@Override
	public boolean containsKey(Object key) {
		return parameters.containsKey(key);
	}

	@Override
	public boolean containsValue(Object value) {
		return parameters.containsValue(value);
	}

	@Override
	public Object get(Object key) {
		return parameters.get(key);
	}

	@Override
	public Object put(String key, Object value) {
		return parameters.put(key, value);
	}

	@Override
	public Object remove(Object key) {
		return parameters.remove(key);
	}

	@Override
	public void putAll(Map<? extends String, ? extends Object> m) {
		parameters.putAll(m);
	}

	@Override
	public void clear() {
		parameters.clear();
	}

	@Override
	public Set<String> keySet() {
		return parameters.keySet();
	}

	@Override
	public Collection<Object> values() {
		return parameters.values();
	}

	@Override
	public Set<java.util.Map.Entry<String, Object>> entrySet() {
		return parameters.entrySet();
	}

	@Override
	public void initialize(String actionName, String pathWithSite) {
	}

	@Override
	public Class getPropertyType(String name) {
		return String.class;
	}

	@Override
	public Class getGenericPropertyType(String name) {
		return String.class;
	}

	@Override
	public String getActionName() {
		return null;
	}

	@Override
	public Collection<String> getProperties() {
		return parameters.keySet();
	}

	@Override
	public void prepareParameter(ActionInvocation invocation) {
	}

	@Override
	public ServiceInfo getServiceInfo() {
		return null;
	}
}